#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_PI_2
#define M_PI_2 3.14159265358979323846/2.0
#endif

#include "QGisWinReleasePdbDemo.h"
#include <QtWidgets/QApplication>
#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include <QVBoxLayout>
#include "qgslayoutview.h"
#include "qgslayoutmanager.h"
#include "qgsproviderregistry.h"
#include <qdebug.h>
#include "qgsrasterlayer.h"
#include "qpushbutton.h"
#include "TestCallback2.h"
#include <Windows.h>
#include "qslider.h"



int main(int argc, char *argv[])
{
    QgsApplication a(argc, argv, true);
    QgsApplication::initQgis();

    QStringList slist = QgsProviderRegistry::instance()->providerList();
    for (auto s1 : slist) {
        qDebug() << s1;
    }

    QGisWinReleasePdbDemo w; //MainWindow
    QgsProject project;// = new QgsProject();
    project.read("E:/data/YanFuLi2024Dec/test2.qgs");
    QTabWidget tabs;

    TestCallback2 testCallback  ;
    {//the map
        //QgsRasterLayer* testLayer = new QgsRasterLayer("url", "test", "testdp");

        QWidget* widget1 = new QWidget; 
        QgsMapCanvas* canvas = new QgsMapCanvas();
        testCallback._mapCanvas = canvas;
        canvas->setProject(&project);
        QList<QgsMapLayer*> layerList;
        QMap<QString, QgsMapLayer*> layersMap = project.mapLayers(true);
        for (auto it = layersMap.begin(); it != layersMap.end(); ++it) {
            layerList.append(it.value());
        }
        //layerList.append(testLayer);
        canvas->setLayers(layerList);
        canvas->zoomToFullExtent();
        canvas->refresh();
        QVBoxLayout* layout = new QVBoxLayout; 
        layout->addWidget(canvas);

        QSlider* slider = new QSlider(Qt::Orientation::Horizontal);
        slider->setMaximum(5000);
        slider->setMinimum(0);
        slider->setValue( 0 );
        slider->connect(slider, &QSlider::valueChanged, &testCallback, &TestCallback2::onSlider);
        layout->addWidget(slider);

        QHBoxLayout* hbox = new QHBoxLayout;

        QPushButton* pluginButton = new QPushButton("start plugin");
        pluginButton->connect(pluginButton, &QPushButton::clicked, &testCallback, &TestCallback2::onUsePlugin);
        hbox->addWidget(pluginButton);

        QPushButton* drawButton = new QPushButton("Draw ROI");
        drawButton->connect(drawButton, &QPushButton::clicked, &testCallback, &TestCallback2::onDrawROI);
        hbox->addWidget(drawButton);

        QPushButton* delRoiButton = new QPushButton("Delete ROI");
        delRoiButton->connect(delRoiButton, &QPushButton::clicked, &testCallback, &TestCallback2::onDelRoi);
        hbox->addWidget(delRoiButton);

        QPushButton* saveButton = new QPushButton("save");
        saveButton->connect(saveButton, &QPushButton::clicked, &testCallback, &TestCallback2::onSave);
        hbox->addWidget(saveButton);

        QPushButton* clearButton = new QPushButton("clear");
        clearButton->connect(clearButton, &QPushButton::clicked, &testCallback, &TestCallback2::onClearButton);
        hbox->addWidget(clearButton);

        layout->addLayout(hbox);
        widget1->setLayout(layout);
        tabs.addTab(widget1, "Map");
    }

    {//layout view 
        QgsLayoutView* layoutView = new QgsLayoutView();
        
        if (project.layoutManager()->printLayouts().size() > 0) {
            QgsPrintLayout* printLayout = * project.layoutManager()->printLayouts().begin();
            layoutView->setCurrentLayout( (QgsLayout*)printLayout);
        }
        QWidget* widget2 = new QWidget;
        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(layoutView);
        widget2->setLayout(layout);
        tabs.addTab(widget2, "Layout");
    }

    

    {
        //test button
        QPushButton* button = new QPushButton("Test!");
        QWidget* widget = new QWidget();
        QVBoxLayout* layout = new QVBoxLayout(widget);
        layout->addWidget(button);
        button->connect(button, &QPushButton::clicked, &testCallback, &TestCallback2::onAddButton);
        QPushButton* clearButton = new QPushButton("Clear"); 
        clearButton->setFixedHeight(64);
        
        layout->addWidget(clearButton);
        layout->addStretch();
        clearButton->connect(clearButton, &QPushButton::clicked, &testCallback, &TestCallback2::onClearButton);
 
        widget->setLayout(layout);
        tabs.addTab(widget, "test");
    }
    
    w.setCentralWidget(&tabs);
 
    w.show();
    return a.exec();
}
